using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace WebAndLoadTestPluginsAndRules.Common
{
    public static class QueryStringParameterCollectionExtensions
    {
        public static void AddQueryStringParameter(this QueryStringParameterCollection queryStringParameters, string parameterName, string parameterValue, bool overwriteIfExists)
        {
            if (queryStringParameters == null)
            {
                throw new ArgumentNullException("queryStringParameters");
            }
            if (String.IsNullOrEmpty(parameterName))
            {
                throw new ArgumentException("parameterName must be specified", "parameterName");
            }
            if (queryStringParameters.Contains(parameterName)) // TODO: Check whether this is/should be case specific
            {
                if (overwriteIfExists)
                {
                    QueryStringParameter parameter = queryStringParameters.Single(p => p.Name == parameterName);
                    parameter.Value = parameterValue;
                }
            }
            else
            {
                queryStringParameters.Add(parameterName, parameterValue);
            }
        }
    }
}